package com.bootcamp.servlet.session;

/**
 * @program: wrok-1101
 * @author: Keyu Li
 * @create: 2021-11-04 14:40
 **/

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(name = "SessionServlet", value = "/session1")
public class SessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Long current = (Long) session.getAttribute("current");
        String id = session.getId();
        if (current!=null){
            PrintWriter writer = response.getWriter();
            writer.println("cookie id: "+id);
            writer.println("last login: "+new Date(current));
        }
        current = System.currentTimeMillis();
        session.setAttribute("current",current);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
